#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project : POS_api_test_project 
@File    : jsonpath_date_replace.py
@IDE     : PyCharm 
@Author  : Mario
@Date    : 2022/5/27 23:27 
@Describe:
"""
import json
import jsonpath


def jsonpath_replace(change_data, key_name):
    """处理jsonpath数据"""
    _new_data = key_name + ''
    for i in change_data:
        if i == '$':
            pass
        elif i[0] == '[' and i[-1] == ']':
            _new_data += "[" + i[1:-1] + "]"
        else:
            _new_data += "[" + "'" + i + "'" + "]"
    return _new_data


def get_json_result(jsonContent, key):
    """
    提取JSON指定内容
    :param jsonContent: JSON内容
    :param key: 提取操作符
    :return: 提取内容
    """
    if type(jsonContent) == str:
        responses = json.loads(jsonContent)
    else:
        # 先把传入的值转换为json格式
        responses = json.loads(jsonContent.content)
    # 将需要提取的值提取出来
    result = jsonpath.jsonpath(responses, "{}".format(key))
    if result:
        if len(result) == 1:
            return result[0]
        return result


def get_json_valus(json_obj, key):
    """
    获取json对应值
    :param json_obj: json字符串
    :param key: 键
    :return:
    """
    valus = dict(json_obj)  # 转化成字典类型
    return valus[key]
